
/**
 * @author jprieur
 */
package mygui;

import java.io.*;
import org.xml.sax.*;
import javax.xml.parsers.*;
import java.awt.event.*;
import javax.swing.*;
import tui.*;
import utils.*;
import kernel.*;

public class ActionReload extends AbstractAction 
{
    private MonIHM _gui;

    public ActionReload(MonIHM gui){
	_gui = gui;

	putValue(AbstractAction.NAME,"Reload");
	putValue(AbstractAction.SHORT_DESCRIPTION,"Reload");
	putValue(AbstractAction.SMALL_ICON,UIM.createImageIcon("images/reload.png","Play"));
    }

    public void actionPerformed(ActionEvent arg0) {
	if(ActionPlay.tmp_file != null){
	    Simulator oldkernel = _gui.getSimulator();
	    Simulator kernel;
	    int old_mode = oldkernel.getMode();
	    
	    
	    if(old_mode == Simulator.SENSOR){
		DataSensorSimulator dss = (DataSensorSimulator)(oldkernel.getProperties());
		kernel = new SensorSimulator(_gui, old_mode);
		kernel.setProperties(dss);
	    }else{
		DataSimulator ds = (DataSimulator)(oldkernel.getProperties());
		kernel = new Simulator(_gui, old_mode);
		kernel.setProperties(ds);
	    }

	    SAXParserFactory factory = SAXParserFactory.newInstance();
	    //factory.setValidating(true); // DTD check
	    // If we want to check with the DTD, the simmobil.dtd file has to be
	    // located near the file

	    try{
		SAXParser parser = factory.newSAXParser();
                if(old_mode==Simulator.SENSOR)
                {
		    parser.parse(ActionPlay.tmp_file, new TuiContentHandler((SensorSimulator)kernel));
                }
                 else
                {
		    parser.parse(ActionPlay.tmp_file, new TuiContentHandler(kernel));
                }
	    } catch (SAXParseException spe) {
		// Error generated by the parser
		System.out.println("\n** Parsing error"
				   + ", line " + spe.getLineNumber()
				   + ", uri " + spe.getSystemId());
		System.out.println("   " + spe.getMessage() );
		// Use the contained exception, if any
		Exception  x = spe;
		if (spe.getException() != null)
		    x = spe.getException();
	    } catch (SAXException sxe) {
		// Error generated by this application
		// (or a parser-initialization error)
		Exception  x = sxe;
		if (sxe.getException() != null)
		    x = sxe.getException();
		x.printStackTrace();
	    } catch (ParserConfigurationException pce) {
		// Parser with specified options can't be built
		pce.printStackTrace();
	    } catch (IOException ioe){
		// I/O error
		ioe.printStackTrace();
	    }


	    _gui.setSimulator(kernel);
	}
	//_gui._display.setMode(DisplayPanel.Mode.ARROW);
	//_gui.updateCommandes();
    }
}
